﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.LevelDataAccess;
using GovernmentOfficeProject.SysTools;

namespace GovernmentOfficeProject.LevelBusinessLogic
{
    public class BLL_Contacts
    {
        DAL_USER_Contacts dalContacts = new DAL_USER_Contacts();
        /// <summary>
        /// 获得联系人数据
        /// </summary>
        /// <param name="contacts"></param>
        /// <param name="pageSize"></param>
        /// <param name="currentPage"></param>
        /// <returns></returns>
        public Grid<Entity_USER_Contacts> getContactsGridData(Entity_USER_Contacts contacts, int pageSize, int currentPage)
        {
            Grid<Entity_USER_Contacts> grid = new Grid<Entity_USER_Contacts>();
            try
            {
                grid.rows = dalContacts.getContactsListPagination(contacts, pageSize, currentPage);
                grid.total = dalContacts.getAllCount(contacts);
            }
            catch(Exception e)
            {
                AppLog.Write(e.Message,AppLog.LogMessageType.Error);
            }
            
            return grid;
        }

         /// <summary>
        /// 获得所有联系人
        /// </summary>
        /// <param name="contacts">联系人对象 ，可做条件查询</param>
        /// <returns></returns>
        public List<Entity_USER_Contacts> getContactsList(Entity_USER_Contacts contacts)
        {
            List<Entity_USER_Contacts> contactsList = null;
            try
            {
                contactsList = dalContacts.getContactsList(contacts);
            }
            catch (Exception e)
            {
                AppLog.Write(e.Message, AppLog.LogMessageType.Error);
            }

            return contactsList;
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="contacts"></param>
        /// <returns></returns>
        public bool saveContacts(Entity_USER_Contacts contacts)
        {
            bool isSuccess = false;
            try
            {
                isSuccess = dalContacts.saveContacts(contacts);     
            }
            catch(Exception e)
            {
                AppLog.Write(e.Message, AppLog.LogMessageType.Error);
            }
            return isSuccess;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool updateContactsById(Entity_USER_Contacts contacts)
        {
            bool isSuccess = false;
            try
            {
                isSuccess = dalContacts.updateContactsById(contacts);
            }
            catch (Exception e)
            {
                AppLog.Write(e.Message, AppLog.LogMessageType.Error);
            }
            return isSuccess;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool deleteContactsById(decimal id)
        {
            bool isSuccess = false;
            try
            {
                isSuccess = dalContacts.deleteContctsById(id);
            }
            catch (Exception e)
            {
                AppLog.Write(e.Message, AppLog.LogMessageType.Error);
            }
            return isSuccess;
        }

        /// <summary>
        /// 验证手机号码是否重复
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="mobileNumber"></param>
        /// <returns></returns>
        public bool validationMobileNumber(string mobileNumber,decimal contactId)
        {
            try
            {
                Entity_USER_Contacts contacts = new Entity_USER_Contacts();
                contacts.UserId = SystemContext.CurrentUserID;
                contacts.MobileNumber = mobileNumber;
                if(contactId > 0)
                {
                    contacts.Id = contactId;
                }
                List<Entity_USER_Contacts> list = dalContacts.getContactsList(contacts);
                return (list != null && list.Count > 0) ? true : false;
                
            } 
            catch (Exception e)
            {
                AppLog.Write(e.Message, AppLog.LogMessageType.Error);
                return false;
            }
        }
    }
}